package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.base.BaseCopywriterConfig;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class BaseCopywriterConfigDAO extends HibernateSupportDelegateDAO<BaseCopywriterConfig> {
    public BaseCopywriterConfigDAO() {
        this.setKeyField("id");
    }

    @DAOMethod(sql = "from BaseCopywriterConfig where id=:id")
    public abstract BaseCopywriterConfig getCopywriterConfigById(@DAOParam("id") int id);

    @DAOMethod(sql = "from BaseCopywriterConfig where objectId=:objectId and moduleId=:moduleId")
    public abstract List<BaseCopywriterConfig> queryCopywriterConfig(@DAOParam("objectId") String objectId,@DAOParam("moduleId") String moduleId);

    @DAOMethod(sql = "from BaseCopywriterConfig where objectId=:objectId")
    public abstract List<BaseCopywriterConfig> queryAllCopywriterConfig(@DAOParam("objectId") String objectId);

    @DAOMethod(sql = "from BaseCopywriterConfig where copywriterCode=:copywriterCode and objectId=:objectId")
    public abstract BaseCopywriterConfig getCopywriterConfigByCode(@DAOParam("copywriterCode") String copywriterCode,@DAOParam("objectId") String objectId);

    @DAOMethod(sql = "from BaseCopywriterConfig where copywriterCode=:copywriterCode and moduleId=:moduleId")
    public abstract BaseCopywriterConfig getCopywriterConfigByCopywriterCode(@DAOParam("copywriterCode") String copywriterCode,@DAOParam("moduleId") String moduleId);
    /**
     * @Describe: 查询文案配置
     * @Param: [moduleId, objectId]
     * @Return: java.util.List<hcn.base.BaseCopywriterConfig>
     * @Author: zhengcb10708
     * @Date: 2021/5/26 9:58
     */
    public List<BaseCopywriterConfig> queryCopywriterConfigByParams(final String moduleId, final String objectId){
        HibernateStatelessResultAction<List<BaseCopywriterConfig>> action = new AbstractHibernateStatelessResultAction<List<BaseCopywriterConfig>>(){
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append(" SELECT id as id ,level as level ,module_id as moduleId ,module_name as moduleName ,copywriter_code as copywriterCode ,copywriter_title as copywriterTitle , ");
                sql.append(" object_id as objectId ,object_name as objectName ,content as content ,picture_id as pictureId ,type as type ,version_no as versionNo ");
                sql.append(" FROM base_copywriter_config  ");
                sql.append(" WHERE  1=1 ");

                if(CommonUtils.isNotEmpty(moduleId)){
                    sql.append(" AND module_id=:moduleId ");
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    sql.append(" AND object_id=:objectId ");
                }
                Query query = ss.createSQLQuery(sql.toString());

                if(CommonUtils.isNotEmpty(moduleId)){
                    query.setParameter("moduleId",moduleId);
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    query.setParameter("objectId",objectId);
                }
                query = (SQLQuery) query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
                List<Map> dbList = query.list();

                List<BaseCopywriterConfig> list = new ArrayList<>();
                if (CommonUtils.isNotEmpty(dbList)) {
                    for (Map map : dbList) {
                        list.add(ConversionUtils.convert(map, BaseCopywriterConfig.class));
                    }
                    setResult(list);
                } else {
                    setResult(null);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        List<BaseCopywriterConfig> list = action.getResult();
        return list;
    }

}